﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-28 14:27:22
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	#region 网办订单 数据访问层
	/// <summary>
	///网办订单 数据访问层
	/// </summary>	
	internal partial class NetworkOrder:INetworkOrder
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.CGTConnectionString;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(OrderID),0)+1
                               FROM [NetworkOrder]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private NetworkOrderInfo GetByReader(IDataReader dr)
         {
            NetworkOrderInfo info = new NetworkOrderInfo();
            object obj;
            obj=dr["OrderID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OrderID = (long)obj;
            }
            obj=dr["BizTypeID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.BizTypeID = Convert.ToInt32(obj);
            }
            obj=dr["CityID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CityID = Convert.ToInt32(obj);
            }
            obj=dr["UserName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UserName = obj.ToString();
            }
            obj=dr["IDNum"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.IDNum = obj.ToString();
            }
            obj=dr["UserMobile"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UserMobile = obj.ToString();
            }
            obj=dr["BeforeMobile"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.BeforeMobile = obj.ToString();
            }
            obj=dr["AfterMobile"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.AfterMobile = obj.ToString();
            }
            obj=dr["PlateNo"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.PlateNo = obj.ToString();
            }
            obj=dr["VIN"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.VIN = obj.ToString();
            }
            obj=dr["IsProvince"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.IsProvince = Convert.ToInt32(obj);
            }
            obj=dr["LicenceClass"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LicenceClass = obj.ToString();
            }
            obj=dr["LowLicenceClass"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LowLicenceClass = obj.ToString();
            }
            obj=dr["DrivingLicenceImage"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.DrivingLicenceImage = obj.ToString();
            }
            obj=dr["DrivingLicenceBackImage"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.DrivingLicenceBackImage = obj.ToString();
            }
            obj=dr["CompulsoryImage"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CompulsoryImage = obj.ToString();
            }
            obj=dr["BeforePlateNoImage"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.BeforePlateNoImage = obj.ToString();
            }
            obj=dr["AfterPlateNoImage"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.AfterPlateNoImage = obj.ToString();
            }
            obj=dr["TemporaryLicenceImage"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.TemporaryLicenceImage = obj.ToString();
            }
            obj=dr["TemporaryLicenceBackImage"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.TemporaryLicenceBackImage = obj.ToString();
            }
            obj=dr["OrderState"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OrderState = Convert.ToInt32(obj);
            }
            obj=dr["PayState"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.PayState = Convert.ToInt32(obj);
            }
            obj=dr["OrderDesc"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OrderDesc = obj.ToString();
            }
            obj=dr["CreatedDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CreatedDate = Convert.ToDateTime(obj);
            }
            obj=dr["UserID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UserID = (long)obj;
            }
            obj=dr["UpdatedDate"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.UpdatedDate = Convert.ToDateTime(obj);
            }
            obj=dr["OrderPrice"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OrderPrice = Convert.ToDecimal(obj);
            }
            obj=dr["NeedPayPrice"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.NeedPayPrice = Convert.ToDecimal(obj);
            }
            obj=dr["CostPrice"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.CostPrice = Convert.ToDecimal(obj);
            }
            obj=dr["ExpressFee"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ExpressFee = Convert.ToDecimal(obj);
            }
            obj=dr["OtherPrice"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.OtherPrice = Convert.ToDecimal(obj);
            }
            obj=dr["MailAddressID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.MailAddressID = (long)obj;
            }
            obj=dr["ExpressCompanyID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.ExpressCompanyID = Convert.ToInt32(obj);
            }
            obj=dr["VehicleLicenceImage"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.VehicleLicenceImage = obj.ToString();
            }
            obj=dr["VehicleLicenceBackImage"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.VehicleLicenceBackImage = obj.ToString();
            }
            obj=dr["HospitalID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.HospitalID = Convert.ToInt32(obj);
            }
            return info;
         }
		public long Insert(NetworkOrderInfo info)
		{
        
        
            string strSql=@"INSERT INTO NetworkOrder			
			(			
			[BizTypeID],
			[CityID],
			[UserName],
			[IDNum],
			[UserMobile],
			[BeforeMobile],
			[AfterMobile],
			[PlateNo],
			[VIN],
			[IsProvince],
			[LicenceClass],
			[LowLicenceClass],
			[DrivingLicenceImage],
			[DrivingLicenceBackImage],
			[CompulsoryImage],
			[BeforePlateNoImage],
			[AfterPlateNoImage],
			[TemporaryLicenceImage],
			[TemporaryLicenceBackImage],
			[OrderState],
			[PayState],
			[OrderDesc],
			[CreatedDate],
			[UserID],
			[UpdatedDate],
			[OrderPrice],
			[NeedPayPrice],
			[CostPrice],
			[ExpressFee],
			[OtherPrice],
			[MailAddressID],
			[ExpressCompanyID],
			[VehicleLicenceImage],
			[VehicleLicenceBackImage],
			[HospitalID]
			)
			 VALUES
			(			
			@BizTypeID,
			@CityID,
			@UserName,
			@IDNum,
			@UserMobile,
			@BeforeMobile,
			@AfterMobile,
			@PlateNo,
			@VIN,
			@IsProvince,
			@LicenceClass,
			@LowLicenceClass,
			@DrivingLicenceImage,
			@DrivingLicenceBackImage,
			@CompulsoryImage,
			@BeforePlateNoImage,
			@AfterPlateNoImage,
			@TemporaryLicenceImage,
			@TemporaryLicenceBackImage,
			@OrderState,
			@PayState,
			@OrderDesc,
			@CreatedDate,
			@UserID,
			@UpdatedDate,
			@OrderPrice,
			@NeedPayPrice,
			@CostPrice,
			@ExpressFee,
			@OtherPrice,
			@MailAddressID,
			@ExpressCompanyID,
			@VehicleLicenceImage,
			@VehicleLicenceBackImage,
			@HospitalID
			)
			 SET @OrderID=@@IDENTITY
			";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddOutParameter(dbc, "@OrderID",DbType.Int64, sizeof(Int64));  
            db.AddInParameter(dbc, "@BizTypeID",DbType.Int32, info.BizTypeID);
            db.AddInParameter(dbc, "@CityID",DbType.Int32, info.CityID);
            db.AddInParameter(dbc, "@UserName",DbType.String, info.UserName);
            db.AddInParameter(dbc, "@IDNum",DbType.String, info.IDNum);
            db.AddInParameter(dbc, "@UserMobile",DbType.String, info.UserMobile);
            db.AddInParameter(dbc, "@BeforeMobile",DbType.String, info.BeforeMobile);
            db.AddInParameter(dbc, "@AfterMobile",DbType.String, info.AfterMobile);
            db.AddInParameter(dbc, "@PlateNo",DbType.String, info.PlateNo);
            db.AddInParameter(dbc, "@VIN",DbType.String, info.VIN);
            db.AddInParameter(dbc, "@IsProvince",DbType.Int32, info.IsProvince);
            db.AddInParameter(dbc, "@LicenceClass",DbType.String, info.LicenceClass);
            db.AddInParameter(dbc, "@LowLicenceClass",DbType.String, info.LowLicenceClass);
            db.AddInParameter(dbc, "@DrivingLicenceImage",DbType.String, info.DrivingLicenceImage);
            db.AddInParameter(dbc, "@DrivingLicenceBackImage",DbType.String, info.DrivingLicenceBackImage);
            db.AddInParameter(dbc, "@CompulsoryImage",DbType.String, info.CompulsoryImage);
            db.AddInParameter(dbc, "@BeforePlateNoImage",DbType.String, info.BeforePlateNoImage);
            db.AddInParameter(dbc, "@AfterPlateNoImage",DbType.String, info.AfterPlateNoImage);
            db.AddInParameter(dbc, "@TemporaryLicenceImage",DbType.String, info.TemporaryLicenceImage);
            db.AddInParameter(dbc, "@TemporaryLicenceBackImage",DbType.String, info.TemporaryLicenceBackImage);
            db.AddInParameter(dbc, "@OrderState",DbType.Int32, info.OrderState);
            db.AddInParameter(dbc, "@PayState",DbType.Int32, info.PayState);
            db.AddInParameter(dbc, "@OrderDesc",DbType.String, info.OrderDesc);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UserID",DbType.Int64, info.UserID);
            db.AddInParameter(dbc, "@UpdatedDate",DbType.DateTime, info.UpdatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@OrderPrice",DbType.Decimal, info.OrderPrice);
            db.AddInParameter(dbc, "@NeedPayPrice",DbType.Decimal, info.NeedPayPrice);
            db.AddInParameter(dbc, "@CostPrice",DbType.Decimal, info.CostPrice);
            db.AddInParameter(dbc, "@ExpressFee",DbType.Decimal, info.ExpressFee);
            db.AddInParameter(dbc, "@OtherPrice",DbType.Decimal, info.OtherPrice);
            db.AddInParameter(dbc, "@MailAddressID",DbType.Int64, info.MailAddressID);
            db.AddInParameter(dbc, "@ExpressCompanyID",DbType.Int32, info.ExpressCompanyID);
            db.AddInParameter(dbc, "@VehicleLicenceImage",DbType.String, info.VehicleLicenceImage);
            db.AddInParameter(dbc, "@VehicleLicenceBackImage",DbType.String, info.VehicleLicenceBackImage);
            db.AddInParameter(dbc, "@HospitalID",DbType.Int32, info.HospitalID);

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (long)db.GetParameterValue(dbc,"OrderID");
         
		}
		
		
        public void Update(NetworkOrderInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE NetworkOrder SET ");
            sbSql.AppendLine("[BizTypeID] = @BizTypeID,");
                
            sbSql.AppendLine("[CityID] = @CityID,");
                
            sbSql.AppendLine("[UserName] = @UserName,");
                
            sbSql.AppendLine("[IDNum] = @IDNum,");
                
            sbSql.AppendLine("[UserMobile] = @UserMobile,");
                
            sbSql.AppendLine("[BeforeMobile] = @BeforeMobile,");
                
            sbSql.AppendLine("[AfterMobile] = @AfterMobile,");
                
            sbSql.AppendLine("[PlateNo] = @PlateNo,");
                
            sbSql.AppendLine("[VIN] = @VIN,");
                
            sbSql.AppendLine("[IsProvince] = @IsProvince,");
                
            sbSql.AppendLine("[LicenceClass] = @LicenceClass,");
                
            sbSql.AppendLine("[LowLicenceClass] = @LowLicenceClass,");
                
            sbSql.AppendLine("[DrivingLicenceImage] = @DrivingLicenceImage,");
                
            sbSql.AppendLine("[DrivingLicenceBackImage] = @DrivingLicenceBackImage,");
                
            sbSql.AppendLine("[CompulsoryImage] = @CompulsoryImage,");
                
            sbSql.AppendLine("[BeforePlateNoImage] = @BeforePlateNoImage,");
                
            sbSql.AppendLine("[AfterPlateNoImage] = @AfterPlateNoImage,");
                
            sbSql.AppendLine("[TemporaryLicenceImage] = @TemporaryLicenceImage,");
                
            sbSql.AppendLine("[TemporaryLicenceBackImage] = @TemporaryLicenceBackImage,");
                
            sbSql.AppendLine("[OrderState] = @OrderState,");
                
            sbSql.AppendLine("[PayState] = @PayState,");
                
            sbSql.AppendLine("[OrderDesc] = @OrderDesc,");
                
            sbSql.AppendLine("[CreatedDate] = @CreatedDate,");
                
            sbSql.AppendLine("[UserID] = @UserID,");
                
            sbSql.AppendLine("[UpdatedDate] = @UpdatedDate,");
                
            sbSql.AppendLine("[OrderPrice] = @OrderPrice,");
                
            sbSql.AppendLine("[NeedPayPrice] = @NeedPayPrice,");
                
            sbSql.AppendLine("[CostPrice] = @CostPrice,");
                
            sbSql.AppendLine("[ExpressFee] = @ExpressFee,");
                
            sbSql.AppendLine("[OtherPrice] = @OtherPrice,");
                
            sbSql.AppendLine("[MailAddressID] = @MailAddressID,");
                
            sbSql.AppendLine("[ExpressCompanyID] = @ExpressCompanyID,");
                
            sbSql.AppendLine("[VehicleLicenceImage] = @VehicleLicenceImage,");
                
            sbSql.AppendLine("[VehicleLicenceBackImage] = @VehicleLicenceBackImage,");
                
            sbSql.AppendLine("[HospitalID] = @HospitalID");
                
            sbSql.Append(" where  [OrderID] = @OrderID");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@OrderID",DbType.Int64, info.OrderID);
            db.AddInParameter(dbc, "@BizTypeID",DbType.Int32, info.BizTypeID);
            db.AddInParameter(dbc, "@CityID",DbType.Int32, info.CityID);
            db.AddInParameter(dbc, "@UserName",DbType.String, info.UserName);
            db.AddInParameter(dbc, "@IDNum",DbType.String, info.IDNum);
            db.AddInParameter(dbc, "@UserMobile",DbType.String, info.UserMobile);
            db.AddInParameter(dbc, "@BeforeMobile",DbType.String, info.BeforeMobile);
            db.AddInParameter(dbc, "@AfterMobile",DbType.String, info.AfterMobile);
            db.AddInParameter(dbc, "@PlateNo",DbType.String, info.PlateNo);
            db.AddInParameter(dbc, "@VIN",DbType.String, info.VIN);
            db.AddInParameter(dbc, "@IsProvince",DbType.Int32, info.IsProvince);
            db.AddInParameter(dbc, "@LicenceClass",DbType.String, info.LicenceClass);
            db.AddInParameter(dbc, "@LowLicenceClass",DbType.String, info.LowLicenceClass);
            db.AddInParameter(dbc, "@DrivingLicenceImage",DbType.String, info.DrivingLicenceImage);
            db.AddInParameter(dbc, "@DrivingLicenceBackImage",DbType.String, info.DrivingLicenceBackImage);
            db.AddInParameter(dbc, "@CompulsoryImage",DbType.String, info.CompulsoryImage);
            db.AddInParameter(dbc, "@BeforePlateNoImage",DbType.String, info.BeforePlateNoImage);
            db.AddInParameter(dbc, "@AfterPlateNoImage",DbType.String, info.AfterPlateNoImage);
            db.AddInParameter(dbc, "@TemporaryLicenceImage",DbType.String, info.TemporaryLicenceImage);
            db.AddInParameter(dbc, "@TemporaryLicenceBackImage",DbType.String, info.TemporaryLicenceBackImage);
            db.AddInParameter(dbc, "@OrderState",DbType.Int32, info.OrderState);
            db.AddInParameter(dbc, "@PayState",DbType.Int32, info.PayState);
            db.AddInParameter(dbc, "@OrderDesc",DbType.String, info.OrderDesc);
            db.AddInParameter(dbc, "@CreatedDate",DbType.DateTime, info.CreatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@UserID",DbType.Int64, info.UserID);
            db.AddInParameter(dbc, "@UpdatedDate",DbType.DateTime, info.UpdatedDate.ToNullDbDateTime());
            db.AddInParameter(dbc, "@OrderPrice",DbType.Decimal, info.OrderPrice);
            db.AddInParameter(dbc, "@NeedPayPrice",DbType.Decimal, info.NeedPayPrice);
            db.AddInParameter(dbc, "@CostPrice",DbType.Decimal, info.CostPrice);
            db.AddInParameter(dbc, "@ExpressFee",DbType.Decimal, info.ExpressFee);
            db.AddInParameter(dbc, "@OtherPrice",DbType.Decimal, info.OtherPrice);
            db.AddInParameter(dbc, "@MailAddressID",DbType.Int64, info.MailAddressID);
            db.AddInParameter(dbc, "@ExpressCompanyID",DbType.Int32, info.ExpressCompanyID);
            db.AddInParameter(dbc, "@VehicleLicenceImage",DbType.String, info.VehicleLicenceImage);
            db.AddInParameter(dbc, "@VehicleLicenceBackImage",DbType.String, info.VehicleLicenceBackImage);
            db.AddInParameter(dbc, "@HospitalID",DbType.Int32, info.HospitalID);
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(long orderID)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [NetworkOrder]  ");
            strSql.Append("WHERE [OrderID]=@OrderID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@OrderID",DbType.Int64, orderID);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<NetworkOrderInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<NetworkOrderInfo> list = new List<NetworkOrderInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "NetworkOrder");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    NetworkOrderInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public NetworkOrderInfo GetByID(long orderID)
		{
            Model.NetworkOrderInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [NetworkOrder] ");
            strSql.Append("WHERE [OrderID]=@OrderID ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@OrderID",DbType.Int64, orderID);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}